import { Toast } from "antd-mobile";

import commonConfig from "../../../config/commonConfig";

import XHQBConfig from "../../../config/XHQBConfig";
import yhTools from "../../../tools/yh";

export const SET_SUPPORTED_BANK_LIST = "SET_SUPPORTED_BANK_LIST";

export const SET_SELECTED_BANK_CARD = "SET_SELECTED_BANK_CARD";

export const ADD_NEW_BANK = "ADD_NEW_BANK";

export const SET_SELECTED_BANK_LIST = "SET_SELECTED_BANK_LIST";

export const setSupportedBankList = data => ({
	type: SET_SUPPORTED_BANK_LIST,
	data
});

export const setSelectedBankList = data => ({
	type: SET_SELECTED_BANK_LIST,
	data
});

export const setSelectedBankCard = data => ({
	type: SET_SELECTED_BANK_CARD,
	data
});

export const addNewBank = data => ({
	type: ADD_NEW_BANK,
	data
});

export const getAllSupportedBankList = ({ channelCode }) => {
	Toast.loading("加载中...", 100);
	let obj = {
		url: commonConfig.supportedBankcardList,
		params: {
			channelCode
		},
		_alertShow: false
	};
	return new Promise(resolve => {
		yhTools.request(obj).then(res => {
			if (res.code === "0") {
				Toast.hide();
				resolve(Array.isArray(res.data) ? res.data : []);
			} else {
				Toast.info(res.message, 2);
			}
		});
	});
};

export const getAllSelectedBankList = () => {
	let obj = {
		url: XHQBConfig.queryQuikpayBankList,
		params: {},
		_alertShow: false
	};
	return new Promise(resolve => {
		yhTools.request(obj).then(res => {
			if (res.code === "0") {
				resolve(Array.isArray(res.data) ? res.data : []);
			} else {
				Toast.info(res.message, 2);
			}
		});
	});
};
/**查询支持的银行列表 */
export const getSupportedBankList = params => {
	return async dispatch => {
		const res = await getAllSupportedBankList(params);
		dispatch(setSupportedBankList(res));
	};
};
/**查询快捷支付银行卡列表 */
export const getSelectedBankList = () => {
	return async dispatch => {
		const res = await getAllSelectedBankList();
		dispatch(setSelectedBankList(res));
	};
};
